package com.cennavi.bigdata.baotou.scripts;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class HanZiToPinYin {
	
	public static void main(String[] args){
		String str = "栆乡三路";
		for (int i = 0; i < str.length(); i++) {
			if(str.charAt(i) == '栆'){
				System.out.println("zhao3");
			}else{
			System.out.println(HanZiToPinYin.toPinYin(str.charAt(i)));
			}
		}
	}
	/**
	 * 返回�?个字的拼�?
	 * @param hanzi
	 * @return
	 */
	public static String toPinYin(char hanzi){
		HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
	    hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	    hanyuPinyin.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
	    hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
	    String[] pinyinArray=null;
	    try {
	    	//是否在汉字范围内
	    	if(hanzi>=0x4e00 && hanzi<=0x9fa5){
	    		pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
	    	}else{
	    		return ToDBC(String.valueOf(hanzi));
	    	}
	    } catch (BadHanyuPinyinOutputFormatCombination e) {
	    	e.printStackTrace();
	    }
	    //将汉字返�?
		return pinyinArray[0];
	}
	
	/**
	 * 半角转全角
	 * 
	 * @param input
	 *            String.
	 * @return 全角字符串.
	 */
	public static String ToSBC(String input) {
		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == ' ') {
				c[i] = '\u3000';
			} else if (c[i] < '\177') {
				c[i] = (char) (c[i] + 65248);

			}
		}
		return new String(c);
	}

	/**
	 * 全角转半角
	 * 
	 * @param input
	 *            String.
	 * @return 半角字符串
	 */
	public static String ToDBC(String input) {

		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == '\u3000') {
				c[i] = ' ';
			} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
				c[i] = (char) (c[i] - 65248);

			}
		}
		String returnString = new String(c);
		return returnString;
	}
}
